define(['jquery', 'bootstrap', 'frontend', 'form', 'template'], function ($, undefined, Frontend, Form, Template) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        index: function () {
          	$("#dsasdadas").click();
            $(document).on("click", ".btn-qq", function () {
                location.href = "/index/index/qqlogin";
            });
            $(document).on("click", ".btn-fqq", function () {
                layer.confirm('确定要解绑QQ吗?', {icon: 3, title:'提示'}, function(index){
                    layer.close(index);
                    Fast.api.ajax({
                        url: "/index/user/fqq",
                    }, function (data, ret) {
                        location.href = ret.url ? ret.url : "/index/user/index";
                    });
                });
            });
        },
      	charge: function () {
            $("#footer").hide();
            Form.api.bindevent($("form"));
        },
        login: function () {
            //本地验证未通过时提示
            $("#login-form").data("validator-options", validatoroptions);

            $(document).on("change", "input[name=type]", function () {
                var type = $(this).val();
                $("div.form-group[data-type]").addClass("hide");
                $("div.form-group[data-type='" + type + "']").removeClass("hide");
                $('#resetpwd-form').validator("setField", {
                    captcha: "required;length(4);integer[+];remote(" + $(this).data("check-url") + ", event=resetpwd, " + type + ":#" + type + ")",
                });
                $(".btn-captcha").data("url", $(this).data("send-url")).data("type", type);
            });

            //为表单绑定事件
            Form.api.bindevent($("#login-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            }, function (data, ret) {
                layer.msg(ret.msg);
              	return false;
            });

            Form.api.bindevent($("#resetpwd-form"), function (data) {
                Layer.closeAll();
            }, function (data, ret) {
                layer.msg(ret.msg);
              	return false;
            });

            $(document).on("click", ".btn-forgot", function () {
                var id = "resetpwdtpl";
                var content = Template(id, {});
                var area = [$(window).width() > 450 ? '450px' : '95%', $(window).height() > 355 ? '355px' : '95%'];
                Layer.open({
                    type: 1,
                    title: __('Reset password'),
                    area: area,
                    content: content,
                    success: function (layero) {
                        Form.api.bindevent($("#resetpwd-form", layero), function (data) {
                            Layer.closeAll();
                        }, function (data, ret) {
                layer.msg(ret.msg);
              	return false;
            });
                    }
                });
            });
        },
        register: function () {
            //本地验证未通过时提示
            $("#register-form").data("validator-options", validatoroptions);
            //为表单绑定事件
            Form.api.bindevent($("#register-form"), function (data, ret) {
                if (!ret.data) {
                    setTimeout(function () {
                        location.href = ret.url ? ret.url : "/";
                    }, 1000);
                }else{
                    $('body').html(ret.data);
                }
            }, function (data, ret) {
                $('img[onclick]').click();
                $('input[name="captcha"]').val('');
                $('input[name="captcha"]').focus();
                layer.msg(ret.msg);
              	return false;
            });
        },
        withdraw: function () {
            //为表单绑定事件
            Form.api.bindevent($("#withdraw-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
            $(document).on("click", ".label-faild", function () {
                var id = $(this).data('id');
                Fast.api.ajax({
                    url: "/index/user/withdraw",
                    type: "post",
                    data: {'id':id},
                }, function (data, ret) {
                    layer.alert(ret.data);
                    return false;
                });
            });
        },
        order: function () {
        },
        profile: function () {
            $("form").data("validator-options", validatoroptions);
            Form.api.bindevent($("form"));
            $(document).on("click", ".btn-api", function () {
                layer.confirm('确定要更换吗?', {icon: 3, title:'提示'}, function(index){
                    layer.close(index);
                    Fast.api.ajax({
                        url: "/api/user/replaceapi",
                    }, function (data, ret) {
                        location.href = ret.url ? ret.url : "/index/user/profile";
                    });
                });
            });
            //为表单绑定事件
            Form.api.bindevent($("#changepwd-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
            //本地验证未通过时提示
            $("#changemobile-form").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#changemobile-form"), function (data, ret) {
                layer.alert('修改成功',function(){
                    location.href = ret.url ? ret.url : "/index/user/index";
                });
            });

            var sendurl = Config.withdraw.sendurl;
            var validateurl = Config.withdraw.validateurl;
            window.setInterval(function(){
                var name = $('input[name="withdraw_name"]').val();
                var account = $('input[name="withdraw_account"]').val();
                var type = $('input[name="withdraw_type"]').val();
                if (name != Config.withdraw.name || account != Config.withdraw.account) {
                    var html = '<div class="row" id="mobile_validate"><div class="form-group"><label class="control-label">手机验证</label><div class="row"><div class="col-sm-8"><input type="text" name="captcha" id="mobile-captcha" class="form-control" data-rule="required;length(4);integer[+];remote('+validateurl+', event=withdraw, mobile:#mobile)"></div> <div class="col-sm-4"><a href="javascript:;" class="btn btn-info btn-captcha" data-url="'+sendurl+'" data-type="mobile" data-event="withdraw">获取验证码</a></div></div></div>';
                    if (!$('.mobile_validate').html()) {
                        $('.mobile_validate').html(html);
                    }
                }else{
                    $('#mobile_validate').remove();
                }
                var select = $('#c-withdraw_type').val();
                if (select == '微信') {
                    $("#withdraw_account").text('OpenID账号');
                }else{
                    $("#withdraw_account").text('收款账号');
                }
            },100);
        }
    };
    return Controller;
});